#include "filehtmldata.h"



FileHTMLData::FileHTMLData(const QString & s, double sz, bool isdir, const QString & r)
{
    _name = s;
    _size = sz;
    _range = r;
    is_dir = isdir;
}


FileHTMLData::FileHTMLData( const FileHTMLData & d)
{
    this->operator =( d );
}


FileHTMLData::FileHTMLData( const FileHTMLData * d)
{
    this->operator =( d );
}


FileHTMLData & FileHTMLData::operator = ( const FileHTMLData *d)
{
    _name = d->name();
    _size = d->size();
    _range = d->range();
    is_dir = d->isDir();
    return *this;
}


FileHTMLData & FileHTMLData::operator = ( const FileHTMLData & d)
{
    _name = d.name();
    _size = d.size();
    _range = d.range();
    is_dir = d.isDir();
    return *this;
}


bool FileHTMLData::operator >( const FileHTMLData * d )
{
    return _size > d->size();
}


bool FileHTMLData::operator >( const FileHTMLData & d )
{
    return _size > d.size();
}


/* strcmp сортировала ни так как мне нада */
int _strcmp(const char *s, const char *d)
{
    char c1 = *s++, c2 = *d++;
    while(c1 && c2)
    {
        if( tolower(c1) > tolower(c2) ) return 1;
        if( tolower(c1) < tolower(c2) ) return -1;

        c1 = *s++;
        c2 = *d++;
    }

    return 0;
}


int FileHTMLData::compare( const FileHTMLData * d)
{
    //return _name.compare(d->name());
    return _strcmp(_name.toUtf8().data(), d->name().toUtf8().data());
}


int FileHTMLData::compare( const FileHTMLData & d)
{
    //return _name.compare(d.name());
    return _strcmp(_name.toUtf8().data(), d.name().toUtf8().data());
}




/***********************************************************/
/** ================ FileHTMLDataList ===================  */
/***********************************************************/


FileHTMLDataList::FileHTMLDataList()
{

}


FileHTMLData *FileHTMLDataList::dirs_at(int i)
{
    if( _dirs.size() <= i ) return 0;
    return &_dirs[i];
}


FileHTMLData *FileHTMLDataList::files_at(int i)
{
    if( _files.size() <= i ) return 0;
    return &_files[i];
}


FileHTMLDataList & FileHTMLDataList::append( const FileHTMLData *d)
{
    if(d->isDir())
        _dirs.append(d);
    else
        _files.append(d);
    return *this;
}


FileHTMLDataList & FileHTMLDataList::append( const FileHTMLData & d)
{
    if(d.isDir())
        _dirs.append(d);
    else
        _files.append(d);
    return *this;
}


FileHTMLDataList & FileHTMLDataList::operator <<( const FileHTMLData *d)
{
    return append(d);
}


FileHTMLDataList & FileHTMLDataList::operator <<( const FileHTMLData & d)
{
    return append(d);
}



void FileHTMLDataList::insert_sort(QVector <FileHTMLData> & d)
{
    FileHTMLData tmp;
    int sz = d.size(), j;

    /* insert sort */
    for(int i = 0; i < sz; ++i)
    {
        tmp = d[i];
        j = i-1;
        while (j >= 0 && d[j].compare(tmp) > 0)
        {
            d[j+1] = d[j];
            j = j-1;
            d[j+1] = tmp;
        }
    }
}


bool FileHTMLDataList::isEmpty()
{
    return (_dirs.isEmpty() && _files.isEmpty());
}


FileHTMLDataList & FileHTMLDataList::sort()
{
    insert_sort(_dirs);
    insert_sort(_files);
    return *this;
}


size_t FileHTMLDataList::dirs_size()
{
    return _dirs.size();
}


size_t FileHTMLDataList::files_size()
{
    return _files.size();
}


size_t FileHTMLDataList::size()
{
    return _dirs.size() + _files.size();
}
